package com.ncloud.common.utils.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import org.apache.poi.util.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

public class TemplateFileUtil {

    public static FileInputStream getTemplates(String tempName) throws FileNotFoundException {
    	// jar包内文件读取无法读取到
    	//return new FileInputStream(ResourceUtils.getFile("classpath:excel-templates/"+tempName));

    	Resource resource = new ClassPathResource("/excel-templates/"+tempName);
    	InputStream inputStream= null;
    	File file = null;
		try {
			inputStream = resource.getInputStream();
			file = File.createTempFile(UUID.randomUUID().toString(), ".xls");
			org.apache.commons.io.FileUtils.copyInputStreamToFile(inputStream, file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
    	return new FileInputStream(file);
    
    }
    
    public static FileInputStream getTemplatesByFilePath(String filePath) throws FileNotFoundException {
        return new FileInputStream(ResourceUtils.getFile(filePath));
    }
}